package com.cyzy.server.service.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class CaptchaCodeFetcher {

    public static void main(String[] args) throws IOException {
        // 服务器的验证码生成接口URL
        String captchaUrl = "http://jwxt.gdufe.edu.cn/jsxsd/verifycode.servlet";

        // 创建URL对象
        URL url = new URL(captchaUrl);

        // 创建HTTP连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法
        connection.setRequestMethod("GET");

        // 获取响应码
        int responseCode = connection.getResponseCode();

        // 如果响应码为200，则读取验证码内容
        if (responseCode == HttpURLConnection.HTTP_OK) {
            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                response.append(line);
            }

            // 关闭Reader
            reader.close();

            // 打印验证码内容
            System.out.println("验证码: " + response.toString());
        } else {
            // 打印请求失败的信息
            System.out.println("请求失败，响应码：" + responseCode);
        }

        // 关闭连接
        connection.disconnect();
    }
}
